package com.zjx.shop.ware.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.util.StringUtils;
import com.zjx.shop.common.utils.PageParams;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjx.shop.common.utils.PageUtils;
import com.zjx.shop.ware.dao.PurchaseDetailDao;
import com.zjx.shop.ware.entity.PurchaseDetailEntity;
import com.zjx.shop.ware.service.PurchaseDetailService;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(PageParams params) {
        QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<>();

        String key  = params.getKey();
        if (!StringUtils.isNullOrEmpty(key)){
            wrapper.and(w -> {
                w.eq("sku_id", key).or().eq("purchase_id", key);
            });
        }

        Integer status  = params.getStatus();
        if (status != null && status != 0){
            wrapper.eq("status", status);
        }

        Long wareId  = params.getWareId();
        if (wareId != null && wareId != 0){
            wrapper.eq("ware_id", wareId);
        }

        IPage<PurchaseDetailEntity> page = this.page(
                new Page<>(params.getPage(),params.getSize()),
                wrapper
        );

        return new PageUtils(page.getCurrent(),page.getSize(),(long) page.getRecords().size(),page.getRecords());
    }

}